package com.younion.action.manage;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Maps;
import com.younion.business.IMemberCardService;
import com.younion.business.IMembercardServiceService;
import com.younion.business.IMsgTemplateService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.Status;
import com.younion.common.constants.TemplateScope;
import com.younion.common.constants.TemplateTargetType;
import com.younion.common.paginator.Paginator;
import com.younion.vo.MemberCardVO;
import com.younion.vo.MembercardServiceVO;
import com.younion.vo.MsgTemplateVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author Joe Chen
*/
public class MembercardServiceAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(MembercardServiceAction.class);

	private IMembercardServiceService membercardServiceService = null;
	
	@Autowired
	private IMemberCardService memberCardService;

	private MembercardServiceVO membercardServiceVO = new MembercardServiceVO();

	private List<MembercardServiceVO> membercardServiceList;// 结果集列表
	
	private List<MsgTemplateVO> serviceTemplates = null;
	
	@Autowired
	private IMsgTemplateService msgTemplateService = null;
	
	private List<TemplateScope> msgTypes = Arrays.asList(TemplateScope.values());
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("editMemberService")
	public String listMembercardService() {
		try {
			Map paramMap = null;
			if (membercardServiceVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(membercardServiceVO));
			}
			// 获取总记录数
			int total = membercardServiceService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			membercardServiceList = membercardServiceService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("editMemberService")
	public String addMembercardService() {
		try {
			/*MemberCardVO memberCardVO = new MemberCardVO(membercardServiceVO.getMemberCard().getId());
			Map paramMap = new HashMap();
			paramMap.put("memberCard", memberCardVO);
			Collection serviceInits = membercardServiceService.getByMap(paramMap);
			MembercardServiceVO serviceInitVO = null;
			Iterator it = serviceInits.iterator();
			while (it.hasNext()) {
				serviceInitVO = (MembercardServiceVO) it.next();
				break;
			}
			if(serviceInitVO != null){
				membercardServiceVO = serviceInitVO;
			}else{
				membercardServiceVO.setMemberCard(memberCardService.getById(membercardServiceVO.getMemberCard().getId().toString()));
			}*/
			membercardServiceVO.setMemberCard(memberCardService.getById(membercardServiceVO.getMemberCard().getId().toString()));
		} catch (Exception e) {
		}
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editMemberService")
	public String editMembercardService() {
		try {
			membercardServiceVO = membercardServiceService.getById(membercardServiceVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("editMemberService")
	public String saveMembercardService() {
		try {
			if(membercardServiceVO.getId() == null){
				MsgTemplateVO template =  msgTemplateService.getById(membercardServiceVO.getTemplate().getId().toString());
				if(!TemplateTargetType.COMMON.equals(template.getTargetType())){
					MembercardServiceVO param = new MembercardServiceVO();
					param.setMemberCard(new MemberCardVO(membercardServiceVO.getMemberCard().getId()));
					Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(membercardServiceVO));
					Collection services = membercardServiceService.getByMap(paramMap);
					for(Iterator<MembercardServiceVO> iterator = services.iterator(); iterator.hasNext();){
						MembercardServiceVO service = (MembercardServiceVO) iterator.next(); 
						if(service.getTemplate().getTargetType().equals(template.getTargetType())){
							this.tipMessage = "该会员已经添加过" + service.getTemplate().getTargetTypeTitle();
							return ERROR;
						}
					}
				}
				this.addLog(3, "新增会员卡计划服务：【"+membercardServiceVO.getTemplate().getTargetType()+
						"】", "新增会员卡计划服务",getSessionUser());
			}else{
				this.addLog(3, "修改会员卡计划服务：【"+membercardServiceVO.getId()+
						"】", "修改会员卡计划服务",getSessionUser());
			}
			membercardServiceVO = membercardServiceService.save(membercardServiceVO);
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("editMemberService")
	public String deleteMembercardService() {
		try {
			String pkId = request.getParameter("key");
			membercardServiceService.remove(Arrays.asList(pkId.split(",")));
			this.addLog(4, "删除会员卡计划服务：ids:【"+pkId+
					"】", "删除会员卡计划服务",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	public String viewMembercardService() {
		try {
			String pkId = request.getParameter("key"); 
			membercardServiceVO = membercardServiceService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}
	

	public List<MsgTemplateVO> getServiceTemplates() {
		if(serviceTemplates == null){
			try{
				Map loadMsgtemplateParamMap = Maps.newHashMap();
				loadMsgtemplateParamMap.put("status", Status.ACTIVE.getName());
				//loadMsgtemplateParamMap.put("targetType1", TemplateTargetType.BIRTHDAY.getName());
				//loadMsgtemplateParamMap.put("targetType2", TemplateTargetType.SILENT.getName());
				loadMsgtemplateParamMap.put("notCommon", TemplateTargetType.COMMON.getName());
				if (getSessionUser().getCustomer() != null) {
					loadMsgtemplateParamMap.put("customerId", getSessionUser().getCustomer().getId());
				}
				serviceTemplates = (List<MsgTemplateVO>) msgTemplateService
						.getByMap(loadMsgtemplateParamMap);
			}catch(Exception e){
				
			}
		}
		return serviceTemplates;
	}

	public void setServiceTemplates(List<MsgTemplateVO> serviceTemplates) {
		this.serviceTemplates = serviceTemplates;
	}

	public MembercardServiceVO getMembercardServiceVO() {
		return membercardServiceVO;
	}

	public void setMembercardServiceVO(MembercardServiceVO membercardServiceVO) {
		this.membercardServiceVO = membercardServiceVO;
	}

	public void setMembercardServiceService(IMembercardServiceService membercardServiceService) {
		this.membercardServiceService = membercardServiceService;
	}

	public List<MembercardServiceVO> getMembercardServiceList() {
		return membercardServiceList;
	}

	public void setMembercardServiceList(List<MembercardServiceVO> membercardServiceList) {
		this.membercardServiceList = membercardServiceList;
	}

	public IMemberCardService getMemberCardService() {
		return memberCardService;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

	public IMembercardServiceService getMembercardServiceService() {
		return membercardServiceService;
	}

	public IMsgTemplateService getMsgTemplateService() {
		return msgTemplateService;
	}

	public void setMsgTemplateService(IMsgTemplateService msgTemplateService) {
		this.msgTemplateService = msgTemplateService;
	}

	public List<TemplateScope> getMsgTypes() {
		return msgTypes;
	}

	public void setMsgTypes(List<TemplateScope> msgTypes) {
		this.msgTypes = msgTypes;
	}
	
	
}